const { WebSocketServer } = require('ws');
const moment = require('moment');
const Mock = require('mockjs');

const { mock } = Mock;

const wss = new WebSocketServer({
  port: 8080,
  path: '/websocket',
});
wss.on('connection', (ws, req) => {
  console.log('客户端已连接：', req.socket);
  ws.on('message', data => {
    console.log('收到客户端发送的消息：', data.toString('utf-8'));
    const obj = {
      key: mock('@id'),
      title: mock('@ctitle'),
      content: mock('@cparagraph'),
      createTime: moment().format('YYYY-MM-DD HH:mm:ss'),
    };
    const msg = JSON.stringify(obj);
    ws.send(msg);
  });
});
